package servlet.DamServlets;

import dao.DamDAO;
import entity.Dam;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Date;


@WebServlet("DoUpdateDamServlet")
public class DoUpdateDamServlet extends HttpServlet {
    @Override
    protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
    doGet(req,resp);
    }

    @Override
    protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        int id = Integer.parseInt(req.getParameter("id"));
        String name = req.getParameter("name");
        String river = req.getParameter("river");
        int capacity = Integer.parseInt(req.getParameter("capacity"));
        String completeDate = req.getParameter("completeDate");
        SimpleDateFormat simpleDateFormat=new SimpleDateFormat("yyyy-MM-dd");
        Date date;
        try {
            date=simpleDateFormat.parse(completeDate);
        } catch (ParseException e) {
            throw new RuntimeException(e);
        }
        Dam dam=new Dam();
        dam.setId(id);
        dam.setName(name);
        dam.setRiver(river);
        dam.setCapacity(capacity);
        dam.setCompleteDate(date);
        DamDAO damDAO=new DamDAO();
        damDAO.update(dam);

        resp.sendRedirect("ViewDams");
    }
}
